﻿using log4net;
using Mangox.BLL.Mangox;
using Mangox.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Web;

namespace MGXAPI.BLL
{
    public class MgxBLL
    {
        protected static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        protected static string className = (new StackTrace()).GetFrame(1).GetMethod().ReflectedType.Name + "/";       //获取类名 

        private static string createAccountUrl = ConfigurationManager.AppSettings["CreateAccountUrl"];
        private static string getBalanceUrl = ConfigurationManager.AppSettings["GetBalanceUrl"];
        private static string bitPool = ConfigurationManager.AppSettings["BitPool"];
        private static string transferUrl = ConfigurationManager.AppSettings["Transfer"];
        private static string gettxsUrl = ConfigurationManager.AppSettings["Gettxs"];
        private static string pubPackageUrl = ConfigurationManager.AppSettings["PubPackage"];
        private static string getPackageUrl = ConfigurationManager.AppSettings["GetPackage"];



        /// <summary>
        /// 创建账户
        /// </summary>
        /// <returns></returns>
        public static string CreateUserHash()
        {
            //var json = "{\"ret\":0,\"res_info\":\"ok\",\"hash\":\"f32aba4171bb641dd23d8d85d28263d7b49c4c2eb0bbcc0465ea687fcf576e31\",\"amount\":0.0}";
            string result = null;
            string url = null;

            try
            {
                JsonRequest request = new JsonRequest();
                url = string.Format(createAccountUrl, bitPool);
                result = request.Get(url);
                log.Info($"创建账号result-：{result}");

                //反转将json返回回dynamic对象
                var model = JsonConvert.DeserializeObject<dynamic>(result);
                if (model != null && model.ret == 0)
                {
                    return model.hash;
                }
            }
            catch (Exception ex)
            {
                log.Error(className + $"CreateMgxUser url-{url}，result-{result}，Fail-{ex.ToString()}");
            }
            return null;
        }

        /// <summary>
        /// 获取余额
        /// </summary>
        /// <returns></returns>
        public string GetBalanceIn(string hash)
        {
            //var json = "{\"ret\":0,\"res_info\":\"ok\",\"hash\":\"\",\"amount\":0.0}";
            string result = null;
            string url = null;

            try
            {
                JsonRequest request = new JsonRequest();
                url = string.Format(getBalanceUrl, bitPool, hash);
                result = request.Get(url);
                log.Info($"查询余额url-：{url}");
                log.Info($"查询余额result-：{result}");

                //反转将json返回回dynamic对象
                var model = JsonConvert.DeserializeObject<dynamic>(result);
                if (model != null && model.ret == 0)
                {
                    return model.amount;
                }
            }
            catch (Exception ex)
            {
                log.Error(className + $"查询余额失败：GetBalance url-{url}，result-{result}，Fail-{ex.ToString()}");
            }
            return null;
        }


        /// <summary>
        /// 给新用户转账，用户金币为0默认转账
        /// </summary>
        /// <returns></returns>
        public double DefaultTransfer(string hash, string toHash, double toAmount)
        {
            double amount = 0;
            string url = null;
            string json = "";

            try
            {
                JsonRequest request = new JsonRequest();
                url = string.Format(transferUrl, bitPool, hash, toHash, toAmount);
                json = request.Get(url);
                log.Info($"默认新用户转账url-：{url}");
                log.Info($"默认新用户转账result-：{json}");

                //反转将json返回回dynamic对象
                var model = JsonConvert.DeserializeObject<dynamic>(json);
                if (model != null && model.ret == 0)
                {
                    amount = model.amount;
                }
                else
                {
                    log.Info(className + $"DefaultTransfer url-{url}，result-{json}");
                }
            }
            catch (Exception ex)
            {
                log.Error(className + $"默认新用户转账：DefaultTransfer url-{url}，result-{amount}，Fail-{ex.ToString()}");
            }
            return 0;
        }

        /// <summary>
        /// 检查Token，无意义，在函数内判断，并使用id
        /// </summary>
        /// <returns></returns>
        public string CheckToken(string t)
        {
            string result = null;

            try
            {
                //if (string.IsNullOrWhiteSpace(t))
                //{
                //    return "没有Token";
                //}

                //解密Token数据
                var token = MgxCommBLL.DecryptToken(t);

                if (token == null || !token.IsToken)
                {
                    return "Token无效";
                }
            }
            catch (Exception ex)
            {
                log.Error(className + $"CheckToken token-{t}，result-{result}，Fail-{ex.ToString()}");
            }
            return null;
        }







    }
}